package com.cmc.cloud.cmclink.doc.controller.admin.si;

import cn.hutool.core.bean.BeanUtil;
import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.convert.BlnoCargoConvert;
import com.cmc.cloud.cmclink.doc.entity.BlnoCargoDO;
import com.cmc.cloud.cmclink.doc.service.si.BlnoCargoService;
import com.cmc.cloud.cmclink.doc.vo.blnocargovo.BlnoCargoCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.blnocargovo.BlnoCargoPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.blnocargovo.BlnoCargoRespVO;
import com.cmc.cloud.cmclink.doc.vo.blnocargovo.BlnoCargoUpdateReqVO;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.operatelog.core.annotations.OperateLog;
import com.cmes.framework.operatelog.core.enums.OperateTypeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;


/**
 * 提单货物信息前端接口Controller
 *
 * @author codegen
 */
@Tag(name = "管理后台 - 提单货物信息")
@RestController
@Validated
@RequestMapping(UrlConstants.APPLICATION_URL + "/blno-cargo")
public class BlnoCargoController {

    @Resource
    private BlnoCargoService blnoCargoService;

    @Operation(summary = "创建提单货物信息")
    @OperateLog(type = OperateTypeEnum.CREATE)
    @PostMapping("/create")
    @PreAuthorize("@ss.hasPermission('doc:blno-cargo:create')")
    public CommonResult<Void> createBlnoCargo(@Valid @RequestBody BlnoCargoCreateReqVO createReqVO) {
        blnoCargoService.createBlnoCargo(createReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "更新提单货物信息")
    @OperateLog(type = OperateTypeEnum.UPDATE)
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermission('doc:blno-cargo:update')")
    public CommonResult<Void> updateBlnoCargo(@Valid @RequestBody BlnoCargoUpdateReqVO updateReqVO) {
        blnoCargoService.updateBlnoCargo(updateReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "删除提单货物信息")
    @OperateLog(type = OperateTypeEnum.DELETE)
    @PostMapping("/delete/{id}")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('doc:blno-cargo:delete')")
    public CommonResult<Void> deleteBlnoCargo(@PathVariable("id") Long id) {
        blnoCargoService.deleteBlnoCargo(id);
        return CommonResult.success();
    }

    @Operation(summary = "获得提单货物信息")
    @OperateLog(type = OperateTypeEnum.GET)
    @GetMapping("/get/{id}")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('doc:blno-cargo:query')")
    public CommonResult<BlnoCargoRespVO> getBlnoCargo(@PathVariable("id") Long id) {
        BlnoCargoDO blnoCargo = blnoCargoService.getBlnoCargo(id);
        return CommonResult.success(BlnoCargoConvert.INSTANCE.convert(blnoCargo));
    }

    @Operation(summary = "获得提单货物信息列表")
    @OperateLog(type = OperateTypeEnum.GET)
    @GetMapping("/list")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('doc:blno-cargo:query')")
    public CommonResult<List<BlnoCargoRespVO>> getBlnoCargoList(@RequestParam("ids") Collection<Long> ids) {
        List<BlnoCargoDO> list = blnoCargoService.getBlnoCargoList(ids);
        return CommonResult.success(BlnoCargoConvert.INSTANCE.convertList(list));
    }

    @Operation(summary = "获得提单货物信息分页")
    @OperateLog(type = OperateTypeEnum.GET)
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('doc:blno-cargo:query')")
    public CommonResult<PageResult<BlnoCargoRespVO>> getBlnoCargoPage(@Valid BlnoCargoPageReqVO pageVO) {
        PageResult<BlnoCargoDO> pageResult = blnoCargoService.getBlnoCargoPage(pageVO);
        List<BlnoCargoRespVO> results = BlnoCargoConvert.INSTANCE.convertList(pageResult.getList());
        return CommonResult.success(new PageResult<>(results, pageResult.getTotal()));
    }

    @Operation(summary = "根据提单号获取提单货物")
    @OperateLog(type = OperateTypeEnum.GET)
    @GetMapping("/getCargoByBlNo")
    @Parameter(name = "blNo", description = "提单号", required = true)
    public CommonResult<List<BlnoCargoRespVO>> getCargoByBlNo(@RequestParam("blNo") String blNo) {
        return CommonResult.success(BeanUtil.copyToList(blnoCargoService.getCargoByBlNo(blNo), BlnoCargoRespVO.class));
    }
}
